import merchantsApi from 'api/merchants'

// state
let state = {
  form: {
    fileVOList: [], // 附件上传,我的
    addressTime: [], // 地点时间，我的
    contentTxt: [], // 描述内容，我的
    projectList: [], // 比赛项目，我的
    fodderList: [], // 附件上传
    rightsDetailList: [{
      exclusiveStatus: 1, // 是否排他：0否 ，1是
      investmentAmount: '', // 招商金额/现金赞助（单位：分）
      realPrice: '', // 实物总价/物品赞助 (单位：分)
      rightsDetails: [], // 子权益列表
      rightsName: '', // 权益名称
      rightsTypeCode: '', // 权益类型
      sponsorType: 1 // 赞助类型/方式
    }],
    sponsorAreaList: [], // 活动地点和活动时间
    sponsorBase: {
      activeLevel: 1, // 活动级别
      activeScale: 1, // 活动规模
      activeType: 1, // 活动类型
      audience: '', // 现场观众
      contactPerson: '', // 联系人
      contactPhone: '', // 联系电话
      endType: 1, // 招商截止
      joinNumber: '', // 参加人数
      logoUrl: '', // 招商logo图片
      title: '' // 招商名称
    },
    sponsorBaseDetails: { // 招商详细信息
      pcDetails: '' // pc端的详细信息
    },
    sponsorOrganizationRespVO: {
      organizersList: [], // 承办方
      sponsorsList: [] // 主办方
    },
    sponsorProjectList: [] // 项目类型
  },
  sport_type: {
    list: []
  },
  merchants_dict: {
    activityTypes: {data: [ // 活动类型
      {value: 1, label: '大众赛事'},
      {value: 2, label: '体育场馆'},
      {value: 3, label: '文化活动'},
      {value: 4, label: '社团组织'},
      {value: 5, label: '个人'},
      {value: 6, label: '校园体育'}
    ]},
    levelTypes: {data: [ // 级别
      {value: 1, label: '世界级'},
      {value: 2, label: '国家级'},
      {value: 3, label: '省市级'},
      {value: 4, label: '县区级'},
      {value: 5, label: '城镇级'}
    ]},
    sponsorTypes: {data: [ // 比赛项目
      {value: 1, label: '现金'},
      {value: 2, label: '物资'},
      {value: 3, label: '现金+物资'}
    ]},
    equitysTypes: {data: [ // 赞助方式
      {value: 1, label: '食品供应商'},
      {value: 2, label: '饮品供应商'},
      {value: 3, label: '交通供应商'},
      {value: 4, label: '服装供应商'},
      {value: 5, label: '冠名权益'},
      {value: 6, label: '其他'}
    ]},
    cutoff: {data: [ // 招商截止时间
      {value: 1, label: '1个月后'},
      {value: 2, label: '3个月后'},
      {value: 3, label: '6个月后'}
    ]},
    activeScales: {data: [
      {value: 1, label: '单一区域'},
      {value: 2, label: '系列赛'}
    ]},
    yesNo: {data: [
      {value: 1, label: '是'},
      {value: 0, label: '否'}
    ]}
  },
  sponsor_merchants_list: {list: [], loaded: false},
  sponsor_merchants_bid_list: {list: [], loaded: false},
  compact_merchants_list: {list: [], loaded: false},
  sponsorship_merchants_list: {list: [], loaded: false},
  fulfil_merchants_list: {list: [], loaded: false}
}

// getters
const getters = {
  merchants_form: state => state.form,
  merchants_dict: state => state.merchants_dict,
  sponsor_merchants_list: state => state.sponsor_merchants_list,
  sponsor_merchants_bid_list: state => state.sponsor_merchants_bid_list,
  compact_merchants_list: state => state.compact_merchants_list,
  sponsorship_merchants_list: state => state.sponsorship_merchants_list,
  fulfil_merchants_list: state => state.fulfil_merchants_list
}

// actions
const actions = {
  sponsor_merchants_list ({ commit, state }, params) { // 招商计划列表
    merchantsApi.merchants_list(params).then((json) => {
      commit('SPONSOR_MERCHANTS_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  compact_merchants_list ({ commit, state }, params) { // 合同管理列表
    merchantsApi.compact_list(params).then((json) => {
      commit('COMPACT_MERCHANTS_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsorship_merchants_list ({ commit, state }, params) { // 合同赞助列表
    merchantsApi.sponsorship_list(params).then((json) => {
      commit('SPONSORSHIP_MERCHANTS_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  fulfil_merchants_list ({ commit, state }, params) { // 合同履行列表
    merchantsApi.fulfil_list(params).then((json) => {
      commit('FULFIL_MERCHANTS_LIST', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_merchants_changestate ({ commit, state }, params) { // 招商计划修改状态
    let data = {
      id: params.row.id,
      merStatus: params.row.merStatus
    }
    merchantsApi.merchants_changestate(data).then((json) => {
      commit('SPONSOR_MERCHANTS_CHANGESTATE', {
        data: true,
        params: params
      })
    }, () => {
      commit('SPONSOR_MERCHANTS_CHANGESTATE', {
        data: false,
        params: params
      })
    })
  },
  pay_merchants_changestate ({ commit, state }, params) {
    let data = {
      id: params.id
    }
    merchantsApi.confirm_pay(data).then((json) => {
      commit('PAY_MERCHANTS_CHANGESTATE', {
        data: data
      })
    })
  },
  merchants_dict ({ commit, state }, params) { // 字典数据
    let url = state.merchants_dict[params].url
    merchantsApi.dict(url).then((json) => {
      commit('MERCHANTS_DICT', {
        data: json,
        params: params
      })
    })
  },
  sport_type ({ commit, state }, params) {
    merchantsApi.sport_type(params).then((json) => {
      commit('SPORT_TYPE', {
        params: params,
        data: json.data
      })
    })
  },
  sponsor_merchants_bid_list ({ commit, state }, params) { // 竞标管理 竞标列表
    merchantsApi.bid_list(params).then((json) => {
      // 处理为每个加上表格名，禁止选中使用
      json.data.forEach(item => {
        item.list.map(j => {
          j.tableName = 'Table' + item.id
          j.exclusiveStatus = item.exclusiveStatus
        })
      })
      commit('SPONSOR_MERCHANTS_BID_LIST', {
        params: params,
        data: json.data
      })
    })
  }
}
// mutations
const mutations = {
  SPONSOR_MERCHANTS_LIST (state, { data, params }) { // 招商列表
    if (data && data.list) {
      data.list.map(i => {
        i.merStatus = i.merStatus.toString() // 开关状态值，vue组件开关识别字符串
      })
      state.sponsor_merchants_list = data
    }
    state.sponsor_merchants_list.loaded = true // 判断第一次loading
  },
  SPONSOR_MERCHANTS_CHANGESTATE (state, { data, params }) { // 招商计划 修改状态
    state.sponsor_merchants_list.list[params.$index].merStatus = data ? (params.row.merStatus) : (params.row.merStatus === '0' ? '1' : '0')
  },
  PAY_MERCHANTS_CHANGESTATE (state, { data, params }) { // 收款修改状态
    state.sponsorship_merchants_list.list.forEach(item => {
      if (item.id === data.id) {
        item.goodsStatus = 2
        item.confirmStatus = 1
      }
    })
  },
  MERCHANTS_DICT (state, { data, params }) { // 通用数据字典方法
    state.merchants_dict[params].data = data
  },
  SPORT_TYPE (state, { data, params }) {
    state.sport_list = data
  },
  COMPACT_MERCHANTS_LIST (state, { data, params }) { // 合同管理列表
    state.compact_merchants_list = data
  },
  FULFIL_MERCHANTS_LIST (state, { data, params }) { // 合同履行列表
    state.fulfil_merchants_list = data
  },
  SPONSORSHIP_MERCHANTS_LIST (state, { data, params }) { // 合同赞助列表
    state.sponsorship_merchants_list = data
  },
  SPONSOR_MERCHANTS_BID_LIST (state, { data, params }) { // 竞标管理，竞标列表
    state.sponsor_merchants_bid_list.list = data
    state.sponsor_merchants_bid_list.activeNames = data[0].id // 默认展开
    // 已开标
    let isNoOpen = 0
    data.forEach(item => {
      if (item.rightsStatus === 1) isNoOpen++
    })
    state.sponsor_merchants_bid_list.isNoOpen = isNoOpen
  },
  SPONSOR_MERCHANTS_BID_REFUSED (state, params) {
    state.sponsor_merchants_bid_list.list[params.lv1].list[params.lv2].bidStatus = 2
  },
  SPONSOR_MERCHANTS_SET_FORM (state) { // 重置/清空formdata表单，点击添加和添加操作里面的提交审核里面所需要
    let obj = {
      fileVOList: [], // 附件上传,我的
      addressTime: [], // 地点时间，我的
      contentTxt: [], // 描述内容，我的
      projectList: [], // 比赛项目，我的
      fodderList: [], // 附件上传
      rightsDetailList: [{
        exclusiveStatus: 1, // 是否排他：0否 ，1是
        investmentAmount: '', // 招商金额/现金赞助（单位：元）
        realPrice: '', // 实物总价/物品赞助 (单位：元)
        rightsDetails: [], // 子权益列表
        rightsName: '', // 权益名称
        rightsTypeCode: '', // 权益类型
        sponsorType: '' // 赞助类型/方式
      }],
      sponsorAreaList: [], // 活动地点和活动时间
      sponsorBase: {
        activeLevel: 1, // 活动级别
        activeScale: 1, // 活动规模
        activeType: 1, // 活动类型
        audience: '', // 现场观众
        contactPerson: '', // 联系人
        contactPhone: '', // 联系电话
        endType: 1, // 招商截止
        joinNumber: '', // 参加人数
        logoUrl: '', // 招商logo图片
        title: '' // 招商名称
      },
      sponsorBaseDetails: { // 招商详细信息
        pcDetails: '' // pc端的详细信息
      },
      sponsorOrganizationRespVO: {
        organizersList: [], // 承办方
        sponsorsList: [] // 主办方
      },
      sponsorProjectList: [] // 项目类型
    }
    let objdata = Object.assign({}, obj)
    for (var attr in objdata) { // 将返回来的数据做跟我的form里面的数据要一一对应进来，只拿form所需要的数据
      state.form[attr] = objdata[attr]
    }
  },
  SPONSOR_MERCHANTS_COOKIES (state, type) {
    let formdata = Object.assign({}, state.form)
    if (type) {
      sessionStorage.formdata = JSON.stringify(formdata) // 将对象转换成字符串存入到sessionStorang
    } else {
      let obj = JSON.parse(sessionStorage.formdata) // 读取到的sessionStorang字符串转换成对象
      state.form = obj
    }
  },
  pay_fn (state, params, type, goodId) {
    console.log(params, 4234234)
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
